<?php

namespace app\admin\model\caijiaku;

use think\Model;
use traits\model\SoftDelete;

class MaterialInventory extends Model
{

    use SoftDelete;

    // 表名
    protected $name = 'material_inventory';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'review_time_text'
    ];
    

    public function base($query)
    {
        $query->alias('material_inventory')->where('material_inventory.caijiakuid',CAIJIAKU_ID)->useSoftDelete($this->deleteTime);
    }

    public static function init()
    {
        self::beforeInsert(function ($row) {
            $row->caijiakuid = CAIJIAKU_ID;
        });
    }



    public function getReviewTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['review_time']) ? $data['review_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setReviewTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function admin()
    {
        return $this->belongsTo('app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


}
